<?php
/**
 * 73YC 限时特购
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
    assign_template();
    $position = assign_ur_here();
    $smarty->assign('page_title', $position['title']);    // 页面标题
    $smarty->assign('top_title', "特价闪购");//顶部标题
    $time  = time();//当前时间
    $day = date("d",$time+7200);

    $_SESSION['area_id'] = 3;
    $sql =  " SELECT l.start_time,l.buy_number, l.end_time,(l.goods_num-l.buy_num) AS goods_stock ,g.goods_name ,g.goods_id, g.shop_price ,l.start_time,l.end_time, g.goods_thumb , g.promote_price , g.market_price ".
            " FROM ".$GLOBALS['ecs']->table('limit') ." AS l ".
            " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON l.goods_id = g.goods_id  ".
            " WHERE l.start_time <= $time AND l.end_time >= $time AND l.area_id = " . $_SESSION['area_id'];
    $limit_goods = $db->getAll($sql);

    //循环区分当前场数与上一场场数
    foreach ($limit_goods as $key => $value) {
        if($day==date("d",$value['start_time']+7200)){
            $promote_end_date                           = $value['end_time'];
            $now_goods[$key]['goods_name']              = $value['goods_name'];
            $now_goods[$key]['promote_end_date_time']   = ($value['end_time']-time());
            $now_goods[$key]['promote_end_date']        = promote_time_out($value['end_time']-time());
            $now_goods[$key]['market_price']            = $value['market_price'];
            $now_goods[$key]['goods_price']             = $value['shop_price']<$value['promote_price']?$value['shop_price']:$value['promote_price'];
            $now_goods[$key]['goods_thumb']             = $value['goods_thumb'];
            $now_goods[$key]['discount']                = number_format($value['market_price']-$value['promote_price'], 2);
            $now_goods[$key]['discount2']               = intval($now_goods[$key]['goods_price']*10/$value['market_price']);
            $now_goods[$key]['url']                     = "goods-".$value['goods_id'].".html";
            $now_goods[$key]['goods_stock']             = $value['goods_stock'];
            $now_goods[$key]['buy_number']              = $value['buy_number'];
        }
        // else{
        //     $promote_end_date_last                      = $value['end_time'];
        //     $last_goods[$key]['goods_name']              = $value['goods_name'];
        //     $last_goods[$key]['market_price']            = $value['market_price'];
        //     $last_goods[$key]['goods_price']             = $value['shop_price']<$value['promote_price']?$value['shop_price']:$value['promote_price'];
        //     $last_goods[$key]['goods_thumb']             = $value['goods_thumb'];
        //     $last_goods[$key]['discount']                = number_format($value['market_price']-$value['promote_price'], 2);
        //     $last_goods[$key]['url']                     = "goods-".$value['goods_id'].".html";
        //     $last_goods[$key]['goods_stock']             = $value['goods_stock'];
        // }
    }


    $day = date("Y-m-d",$time);  //获取当前时间
    $day_last2_ten=strtotime($day)-93600;//获取前两天22点的时间戳
    $day_last_ten=strtotime($day)-7200;//获取昨天22点的时间戳
    $day_now_ten=strtotime($day)+79200;//获取当天22点的时间戳
    $day_now_eleven=strtotime($day)+82800;//获取当天23点的时间戳
    $day_now_twelve=strtotime($day)+86400;//获取当天24点的时间戳
    $day_next_ten=strtotime($day)+165600;//获取明天22点的时间戳

     /* 获得当天闪购产品 */
     if($time < $day_now_ten){
        $today_time =$day_now_ten;
     }
     else{
         $today_time =$day_now_ten;
     }


    $sql =  " SELECT g.goods_name , g.shop_price , g.goods_id, l.start_time,l.end_time, g.goods_thumb , g.promote_price , g.market_price , (l.goods_num-l.buy_num) AS goods_stock ".
            " FROM ".$GLOBALS['ecs']->table('goods') ." AS g ".
            " LEFT JOIN ".$GLOBALS['ecs']->table('limit')." AS l ON l.goods_id = g.goods_id AND l.area_id = ".$_SESSION['area_id'].
            " WHERE l.start_time = $today_time ";
    $today_goods = $db->getAll($sql);
    foreach ($today_goods as $key => $value) {
        $today_time                                  = $value['start_time'];
        $today_goods[$key]['goods_name']              = $value['goods_name'];
        $today_goods[$key]['market_price']            = $value['market_price'];
        $today_goods[$key]['goods_price']             = $value['shop_price']<$value['promote_price']?$value['shop_price']:$value['promote_price'];
        $today_goods[$key]['goods_thumb']             = $value['goods_thumb'];
        $today_goods[$key]['discount']                = number_format($value['market_price']-$value['promote_price'], 2);
        $today_goods[$key]['url']                     = "goods-".$value['goods_id'].".html";
        $today_goods[$key]['goods_stock']             = $value['goods_stock'];
        $today_goods[$key]['discount2']               = intval($today_goods[$key]['goods_price']*10/$value['market_price']);
        $today_goods[$key]['promote_end_date_time']   = ($value['end_time']-time());
        $today_goods[$key]['yijian_price']            = $value['market_price'] - $today_goods[$key]['goods_price'];
        $today_end_time                               = ($value['end_time']-time())*1000; //当天倒计时
        $today_end_time_ten                           = ($value['end_time']-time())*1000 - 3600000;//当天到十点开始倒计时
      }
      $smarty->assign('today_end_time', $today_end_time);
      $smarty->assign('today_end_time_ten', $today_end_time_ten);
    /* 获取昨天闪购商品 */
    if($time > $day_last_ten){
        $over_time = $day_last_ten;
    }
    $sql =  " SELECT g.goods_name , g.shop_price , g.goods_id, l.start_time,l.end_time, g.goods_thumb , g.promote_price , g.market_price , (l.goods_num-l.buy_num) AS goods_stock ".
            " FROM ".$GLOBALS['ecs']->table('goods') ." AS g ".
            " LEFT JOIN ".$GLOBALS['ecs']->table('limit')." AS l ON l.goods_id = g.goods_id AND l.area_id = ".$_SESSION['area_id'].
            " WHERE l.start_time = $over_time ";
    $over_goods = $db->getAll($sql);

    foreach ($over_goods as $key => $value) {
        $over_time                                   = $value['start_time'];
        $over_goods[$key]['goods_name']              = $value['goods_name'];
        $over_goods[$key]['shop_price']              = $value['shop_price'];
        $over_goods[$key]['promote_price']           = $value['promote_price'];
        $over_goods[$key]['goods_price']             = $value['shop_price'] < $value['promote_price'] ? $value['shop_price'] : $value['promote_price'];
        $over_goods[$key]['goods_thumb']             = $value['goods_thumb'];
      }

    //获取下下场预告
        $next_time2 = $day_next_ten;
        $sql =  " SELECT g.goods_name , g.shop_price , g.goods_id, l.start_time,l.end_time, g.goods_thumb , g.promote_price , g.market_price , (l.goods_num-l.buy_num) AS goods_stock ".
                " FROM ".$GLOBALS['ecs']->table('goods') ." AS g ".
                " LEFT JOIN ".$GLOBALS['ecs']->table('limit')." AS l ON l.goods_id = g.goods_id AND l.area_id = ".$_SESSION['area_id'].
                " WHERE l.start_time = $next_time2 ";
        $next_goods2 = $db->getAll($sql);
        foreach ($next_goods2 as $key => $value) {
            $next_time2                      = $value['start_time'];
            $next_goods2[$key]['goods_name']              = $value['goods_name'];
            $next_goods2[$key]['market_price']            = $value['market_price'];
            $next_goods2[$key]['goods_price']             = $value['shop_price']<$value['promote_price']?$value['shop_price']:$value['promote_price'];
            $next_goods2[$key]['goods_thumb']             = $value['goods_thumb'];
            $next_goods2[$key]['discount']                = number_format($value['market_price']-$value['promote_price'], 2);
            $next_goods2[$key]['url']                     = "goods-".$value['goods_id'].".html";
            $next_goods2[$key]['goods_stock']             = $value['goods_stock'];
            $next_end_time                               = ($value['end_time']-time())*1000;
        }
        $smarty->assign('next_end_time', $next_end_time);
        $smarty->assign('next_time2', ($next_time2-time()));//预告剩余时间戳
        $smarty->assign('next_time_date2', promote_time_out($next_time2-time()));//预告剩余时间
        $smarty->assign('next_goods2', $next_goods2);
    $smarty->assign('next_time', ($next_time-time()));//预告剩余时间戳
    $smarty->assign('next_time_date', promote_time_out($next_time-time()));//预告剩余时间
    $smarty->assign('promote_end_date_time', ($promote_end_date-time()));//本场剩余时间戳
    $smarty->assign('promote_end_date', promote_time_out($promote_end_date-time()));//本场剩余时间
    $smarty->assign('next_goods', $next_goods);
    $smarty->assign('now_goods', $now_goods);
    $smarty->assign('today_goods', $today_goods);
    $smarty->assign('over_goods', $over_goods);
    $smarty->assign('day_last2_ten', $day_last2_ten);
    $smarty->assign('day_last_ten', $day_last_ten);
    $smarty->assign('day_now_ten', $day_now_ten);
    $smarty->assign('day_next_ten', $day_next_ten);
    $smarty->assign('day_now_eleven', $day_now_eleven);
    $smarty->assign('day_now_twelve', $day_now_twelve);
    $smarty->assign('time', $time);
    $smarty->assign('script_name', 'flash');
    $smarty->display('flash.dwt');

/*格式化时间戳函数*/
function  time_start($data){
        $Y = '';
        $D = '';
        $H = '';
        $M = '';
        $T = '';
        if(intval($data/3600)!=0){
          $H=intval($data/3600)."时";
        }else if($D!=''){
          $H='0'."时";
        }
        if(intval($data%(3600)/60)!=0){
          $M=intval((($data%(3600))/60))."分";
        }else if($H!=''){
          $M='0'."分";
        }
        if(intval($data%60)!=0){
          $T=intval($data%60)."秒";
        }else if($M!=''){
          $T='0'."秒";
        }
        return $Y.$D.$H.$M.$T;
      }
/*格式话时间*/
function get_time($data){
        $pic_time['hours']=intval($data/3600);
        $pic_time['minutes']=intval((($data%(3600))/60));
        $pic_time['seconds']=intval($data%60);
        $pic_time['seconds_1']="time_".floor($pic_time['seconds']/10);
        $pic_time['seconds_2']="time_".floor($pic_time['seconds']%10);
        $pic_time['minutes_1']="time_".floor($pic_time['minutes']/10);
        $pic_time['minutes_2']="time_".floor($pic_time['minutes']%10);
        $pic_time['hours_1']="time_".floor($pic_time['hours']/10);
        $pic_time['hours_2']="time_".floor($pic_time['hours']%10);
        return $pic_time;
}
/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */
/**
 *
 *
 * @access public
 * @param
 *
 * @return void
 */
function is_not_null($value)
{
    if (is_array($value))
    {
        return (!empty($value['from'])) || (!empty($value['to']));
    }
    else
    {
        return !empty($value);
    }
}

/**
 * 获得预售商品
 *
 * @access  public
 * @return  array
 */
function get_forecast_goods($num=null)
{
    $time = gmtime();
    $order_type = $GLOBALS['_CFG']['recommend_order'];

    /* 取得促销lbi的数量限制 */
    if(empty($num)){
        $num = get_library_number("recommend_promotion");
    }

    $sql = 'SELECT g.goods_id, g.goods_name, g.promote_start_date, g.goods_thumb, g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
            " AND g.is_promote = 1 AND g.promote_start_date > '$time'";

    $sql .= $order_type == 0 ? ' ORDER BY g.promote_start_date ASC' : ' ORDER BY rnd';
    $sql .= " LIMIT $num ";
    $result = $GLOBALS['db']->getAll($sql);

    $goods = array();
    foreach ($result AS $idx => $row)
    {
        $goods[$idx]['goods_id']           = $row['goods_id'];
        $goods[$idx]['goods_name']         = $row['goods_name'];
        $goods[$idx]['promote_start_date'] = local_date('Y-m-d H:i',$row['promote_start_date'] );
        $goods[$idx]['thumb']              = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $goods[$idx]['goods_img']          = get_image_path($row['goods_id'], $row['goods_img']);
        $goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
    }
    return $goods;
}

?>
